package com.gmgx.hrms.dao.impl;

import com.gmgx.hrms.dao.EmpDao;
import com.gmgx.hrms.dao.ResignDao;
import com.gmgx.hrms.entity.Emp;
import com.gmgx.hrms.entity.Resign;
import com.gmgx.hrms.util.JDBCTools;

import java.rmi.registry.Registry;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ResignDaoImpl implements ResignDao {

    private JDBCTools tools=new JDBCTools();

    @Override
    public boolean queryResignByEmpId(Integer empid) {
        boolean flag=false;
       ResultSet rs= tools.query("select * from t_resign where resign_emp=? and resign_status!=3",empid);
        try {
            if(rs.next()){
                flag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return flag;
    }

    @Override
    public boolean addResignForEmp(Resign resign) {
        int i=tools.update("insert into t_resign(resign_emp,resign_reason,resign_date,resign_status) values(?,?,?,?)",
                resign.getResign_emp().getEmp_id(),resign.getResign_reason(),resign.getResign_date(),resign.getResign_status());
        return i>0;
    }

    @Override
    public List<Resign> queryResignByStatus(String status) {
        List<Resign> list=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_resign where resign_status=?",status);
        try {
            while(rs.next()){
                Resign r=new Resign();
                 r.setResign_id(rs.getInt("resign_id"));
                 r.setResign_status(rs.getString("resign_status"));
                 r.setResign_date(rs.getDate("resign_date").toLocalDate());
                EmpDao dao=new EmpDaoImpl();
               Emp emp= dao.queryEmpById(rs.getInt("resign_emp"));
                 r.setResign_emp(emp);
                 r.setResign_reason(rs.getString("resign_reason"));

                list.add(r);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;
    }

    @Override
    public boolean updateResignStatusByResignId(int resignid,String status) {
       int i=  tools.update("update t_resign set resign_status=? where resign_id=? ",status,resignid);
        return i>0;
    }
}
